<%@page import="com.entor.dbutil.Databaseutil"%>
<%@page import="com.entor.sql.TopicDaoimpl"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>


<!DOCTYPE html >
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<link href="css/main.css#" rel="stylesheet" type="text/css">
<script type="text/javascript">
	function check(){
		var login_username = document.getElementById("uname");
		var login_password = document.getElementById("upwd");
		if(login_username.value == ""){
			alert("用户名不能为空！请重新填入！");
			login_username.focus();	
			return false;
		}else if(login_password.value == ""){
			alert("密码不能为空！请重新填入！");
			login_password.focus();
			return false;
		}
		return true;
	}
	
	function focusOnLogin(){
		var login_username = document.getElementById("uname");
		login_username.focus();	
	}
</script>
</head>
<body>
	<header>
		<div id="top_login">
			<form action="util/user" method="post" onsubmit="return check()">
				<input type="hidden" name="opr" value="login" /> <label> 登录名
				</label> <input type="text" name="uname" id="uname" value=""
					class="login_input" /> <label> 密&#160;&#160;码 </label> <input
					type="password" name="upwd" id="upwd" value="" class="login_input" />
				<input type="submit" class="login_sub" value="登录" /> <label
					id="error"> </label> <img src="images/friend_logo.gif" alt="Google"
					id="friend_logo" />
			</form>
		</div>
		<div id="nav">
			<div id="logo">
				<img src="images/logo.jpg" alt="新闻中国" />
			</div>
			<div id="a_b01">
				<img src="images/a_b01.gif" alt="" />
			</div>
			<!--mainnav end-->
		</div>
	</header>
	<div id="container">

		<%@ include file="index-elements/index_sidebar.jsp"%>

		<main>
			<div class="class_type">
				<img src="images/class_type.gif" alt="新闻中心" />
			</div>
			<div class="content">
				<ul class="class_date">
					
				</ul>
				<ul class="classlist">
					<%
					//新闻分类
						TopicDaoimpl tdi = new TopicDaoimpl();
					//获取所有的新闻分类
						List<Topic> topics = tdi.getAllTopic();
						for(Topic topic:topics){
							%>
								<a href="index.jsp?tid=<%=topic.getTid()%>">
								<%=topic.getTname() %></a>
							<%
						}
					%>
				</ul>
				<ul class="classlist">
					<%
					//新闻，如果是分类的，那就按分类显示
					//把总页数保存到session中,从request中获取要跳转的页面
					//把页码以及页数，新闻数保存到一个对象中，在保存到session中
					//HttpSession session = request.getSession();
					//如果不是第一次请求，通过session获取信息
						Pagenum pagenum = (Pagenum)session.getAttribute("pagenum");
						//存放新闻数量信息
						
						//新闻分类
						NewsDaoimpl ndis = new NewsDaoimpl();
						//如果是第一次请求，自己创建Pagenum
						if(pagenum==null){
							//获取新闻总数
							int newnum = ndis.getNewsNum();
							pagenum = new Pagenum(newnum,(newnum-1)/10+1,1);
							session.setAttribute("pagenum", pagenum);
						}
						//获取要跳转的页码
						String indexidstr = request.getParameter("indexid");
						int indexid ;
						//如果没有提交信息，默认为第一页
						if(indexidstr==null){
							indexid = 1;
						}else{
							
							indexid = Databaseutil.stringtoint(indexidstr);
							pagenum.setNewsum(indexid);
						}
						//移除原来的pagenum
						session.removeAttribute("pagenum");
						//把当前页面设置进pagenum中
						pagenum.setPagenum(indexid);						
						session.setAttribute("pagenum", pagenum);
						//新闻分类
						String tidstr = request.getParameter("tid");
						
						List<News> newlist = null;
						if(tidstr==null){
							//当前页新闻
							newlist = ndis.getPaging((pagenum.getPagenum()-1)*10, 10);							
						}else{
							int tid = Databaseutil.stringtoint(tidstr);
							//如果是分类的新闻重新设置新闻总数
							pagenum.setNewsum(ndis.getNewsNum(tid));							
							newlist = ndis.getPaging((pagenum.getPagenum()-1)*10,10,tid);
						}
												
						count = 1;
						for(News news:newlist){
							%>
								<li><a href="util/news?rep=readNew&nid=<%=news.getNid()%>">
								<%=news.getNtitle() %></a>
								<span><%=news.getNcreateDate() %></span>
								</li>
							<%
							if(count%4==0){
								out.print("<br/>");
							}
							count++;
						}
					%>
					<p align="center">当前页数:&nbsp;<%=pagenum.getPagenum() %>/<%=pagenum.getPagesum() %>
						
						<%
							if(pagenum.getPagenum()==1){
								%>
									<a href="index.jsp?indexid=2">下一页</a>
									<a href="index.jsp?rep=listtitle&indexid=<%=pagenum.getPagesum()%>"
									>末页</a>
								<%
							}else if(pagenum.getPagenum()==pagenum.getPagesum()){
								%>
									<a href="index.jsp?rep=listtitle&indexid=1">首页</a>
									<a href="index.jsp?rep=listtitle&indexid=<%=pagenum.getPagesum()-1%>"
									>上一页</a>
								<%
							}else if(pagenum.getNewsum()<=10){
																
							}else{
								%>
									<a href="index.jsp?rep=listtitle&indexid=1">首页</a>
									<a href="index.jsp?rep=listtitle&indexid=<%=pagenum.getPagenum()-1%>">上一页</a>
									<a href="index.jsp?rep=listtitle&indexid=<%=pagenum.getPagenum()+1%>">下一页</a>
									<a href="index.jsp?rep=listtitle&indexid=<%=pagenum.getPagesum()%>">末页</a>
								<%
							}
						%>
						
					</p>
				</ul>
			</div>
			<%@ include file="index-elements/index_rightbar.html"%>
		</main>
	</div>
	<%@ include file="index-elements/index_bottom.html"%>
</body>
</html>